package com.zhiyou100.taxi.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhiyou100.taxi.dao.CarMapper;
import com.zhiyou100.taxi.entity.Car;
import com.zhiyou100.taxi.entity.CarCheck;
import com.zhiyou100.taxi.service.CarCheckService;
import com.zhiyou100.taxi.service.CarInService;
import com.zhiyou100.taxi.service.CarOutService;
import com.zhiyou100.taxi.service.CarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Set;

/**
 * @author zhaokuii11@163.com
 * @create 2021-11-24 22:01
 * @Description
 */
@Transactional
@Service
public class CarServiceImpl implements CarService {
    @Autowired
    CarMapper carMapper;

    @Override
    public List<Car> getAll() {
        List<Car> list = carMapper.selectByExample(null);
        return list;
    }

    @Override
    public PageInfo<Car> queryAllCar(Integer page, Integer limit, Integer status) {
        PageHelper.startPage(page, limit);
        List<Car> list = getAll();
        PageInfo<Car> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public PageInfo<Car> queryPageCar(Integer pageNum) {
        PageHelper.startPage(pageNum, 5);
        List<Car> list = getAll();
        PageInfo<Car> carPageInfo = new PageInfo<>(list);
        return carPageInfo;
    }

    @Autowired
    CarInService carInService;
    @Autowired
    CarOutService carOutService;
    @Autowired
    CarCheckService carCheckService;


    //删除外键也要删除
    @Override
    public int deleteByPrimaryKeyOne(String carId) {
        carInService.deleteByCarId(carId);
        carOutService.deleteByCarId(carId);
        carCheckService.deleteByCarId(carId);
        return carMapper.deleteByPrimaryKey(carId);

    }

    @Override
    public Car getCarByPrimaryKeyOne(String carId) {
        Car car = carMapper.selectByPrimaryKey(carId);
        return car;
    }

    @Override
    public int updateCarOne(Car car) {
        int i = carMapper.updateByPrimaryKeySelective(car);
        return i;
    }

    @Override
    public int saveOne(Car car) {
        return carMapper.insertSelective(car);
    }

    @Override
    public String getPrimaryKey() {
        int key = carMapper.selectPrimaryKey();
        return "car_" + key;
    }

    @Override
    public int deleteByPrimarySet(Set<String> set) {
        try {
            set.forEach((e) -> {
                carInService.deleteByCarId(e);
                carOutService.deleteByCarId(e);
                carCheckService.deleteByCarId(e);
                carMapper.deleteByPrimaryKey(e);
            });
            return set.size();
        } catch (Exception e) {
            e.printStackTrace();
        }


        return 0;
    }
}
